package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import com.icss.daos.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.icss.daos.ReplyDao;

public class bbsDealServlet extends HttpServlet {

	/**
	 * 处理提交来的请求
	 * 管志春
	 * 2010-03-17
	 */
	public bbsDealServlet() {
		super();
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doPost(request, response);
	}

	//调用dopost方法处理请求
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		boolean flag;		//标记
		
		
		bbsUserDao bbsusedao = new bbsUserDao();
		ClassDao classDao = new ClassDao();
		BoardDao boardDao = new BoardDao();
		ArticleDao articleDao = new ArticleDao();
		ReplyDao replyDao = new ReplyDao();
		
		
		String param = request.getParameter("param");
		
		System.out.println(param);
		
		//回复请求
		if(param.equals("reply")){
		
		
		int aid = Integer.parseInt(request.getParameter("aid"));
		int bid = (Integer)session.getAttribute("bid");
		String replyName = (String)session.getAttribute("userName");
		String replyBody = request.getParameter("cont");
		
		
		
		String sql = "insert into Reply(bid,aid,replyerName,replyBody,replyTime) values("+bid+","+aid+",'"+replyName+"','"+replyBody+"',default)";

		replyDao.executeSQL(sql);

		replyDao.close();
		
		request.getRequestDispatcher("/BBS/tiezi.jsp").forward(request, response);
		}
		
		//添加用户请求
		if(param.equals("addUser")){

			//int bid = Integer.parseInt(request.getParameter("bid"));
			String userName = request.getParameter("userName");
			String userPwd = request.getParameter("userPwd");
			String sex = request.getParameter("sex");
			String address = request.getParameter("address");
			String birthday = request.getParameter("birthday");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String msn = request.getParameter("msn");
			String face = request.getParameter("face");
			
			String sql = "insert into BBSUser(pid,userName,userPwd,registerTime,sex,address,birthday,email,phone,MSN,face)" +
					" values(default,'"+userName+"','"+userPwd+"',default,'"+sex+"','"+address+"','"+birthday+"','"+email+"'" +
							",'"+phone+"','"+msn+"','"+face+"')";
			
			flag = bbsusedao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('添加成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('添加失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
			
			
		}
		
		//添加文章请求
		if(param.equals("addArticle")){
			
			
			int bid = (Integer)session.getAttribute("bid");
			String authorName = (String)session.getAttribute("userName");
			int boardId = Integer.parseInt(request.getParameter("boardId"));
		
			String articleName = request.getParameter("articleName");
			String articleBody = request.getParameter("cont");
		
			String sql = "insert into Article(bid,boardID,articleName,articleBody,createTime,authorName) " +
					"values("+bid+","+boardId+",'"+articleName+"','"+articleBody+"',default,'"+authorName+"')";
			
			flag = articleDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('添加成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('添加失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
			//request.setAttribute("boardId", boardId);
			
		}
		
		

		//添加二级版块
		if(param.equals("addBoard")){
			
			int classId = Integer.parseInt(request.getParameter("classId"));
			String boardName = request.getParameter("boardName");
			String boardMsg = request.getParameter("boardMsg");
			String sql = "insert into Board(classId,boardName,boardMsg) values("+classId+",'"+boardName+"','"+boardMsg+"')"; 
			
			flag = boardDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('添加成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('添加失败');location.href='javascript:history.go(-1)'</script>");
				}
			
			out.close();
			
			
			
		}
		
		//删除回帖
		if(param.equals("deleteReply")){
			
			
			Object pid = session.getAttribute("pid");
			if(pid==null || (Integer)pid==1){
				
				out.write("<script>alert('你不是论坛管理员！无权此操作！！！');location.href='javascript:history.go(-1)'</script>");
				
			}else{
				
			int replyId = Integer.parseInt(request.getParameter("replyID"));
			String sql = "delete from Reply where rid="+replyId;
			
			
			flag = replyDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('删除成功');location.href='javascript:history.go(-1)'</script>");
			}else{
				out.write("<script>alert('删除失败');location.href='javascript:history.go(-1)'</script>");
				}
			

			}
			out.close();
		}
		
		//删除文章
		if(param.equals("deleteArticle")){
			
			
			Object pid = session.getAttribute("pid");
			if(pid==null || (Integer)pid==1){
				
				out.write("<script type='text/javascript'>alert('你不是论坛管理员！无权此操作！！！');location.href='javascript:history.go(-1)'</script>");
				
			}else{
				
			int aid = Integer.parseInt(request.getParameter("aid"));
			String sql = "delete from Article where aid="+aid;
			
			
			flag = articleDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('删除成功');location.href='javascript:history.go(-1)'</script>");
			}else{
				out.write("<script>alert('删除失败');location.href='javascript:history.go(-1)'</script>");
				}
			
			
			
			}
			out.close();
		}
		
		//删除耳二级版块
		if(param.equals("deleteBoard")){
			
			Object pid = session.getAttribute("pid");
			if(pid==null || (Integer)pid==1){
				
				request.getRequestDispatcher("/BBS/index.jsp").forward(request,response);
				
			}else{
				
			
			int boardId = Integer.parseInt(request.getParameter("boardId"));
			String sql = "delete from Article where articleId="+boardId;
			

			flag = boardDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('删除成功');location.href='javascript:history.go(-1)'</script>");
			}else{
				out.write("<script>alert('删除失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
			
			
			}
		}
		
		//更新二级版块信息
		if(param.equals("updateBoard")){
			
			int boardID = Integer.parseInt(request.getParameter("boardID"));
			String  boardName = request.getParameter("boardName");
			int classID = Integer.parseInt(request.getParameter("classId"));
			String boardMsg = request.getParameter("boardMsg");
			String sql = "update Board set boardName='"+boardName+"',classID="+classID+",boardMsg='"+boardMsg+"' where boardID="+boardID;
			flag = bbsusedao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('修改成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('修改失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
		}
		
		//更新文章信息
		if(param.equals("updateArticle")){
			

			//int articleId = Integer.parseInt(request.getParameter("articleId"));
			int aid = Integer.parseInt(request.getParameter("aid"));
			String articleName = request.getParameter("articleName");
			String articleBody = request.getParameter("articlebody");
			String authorName = request.getParameter("author");
			String sql = "update Article set articlename='"+articleName+"',articleBody='"+articleBody+"' where aid="+aid;
			
			flag = articleDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('修改成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('修改失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
			
			
			
		}
		
		//更新用户信息
		if(param.equals("updateUserInfo")){
			
			//int articleId = Integer.parseInt(request.getParameter("articleId"));
			int bid = Integer.parseInt(request.getParameter("bid"));
			String userName = request.getParameter("userName");
			String userPwd = request.getParameter("userPwd");
			String sex = request.getParameter("sex");
			String address = request.getParameter("address");
			String birthday = request.getParameter("birthday");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String msn = request.getParameter("msn");
			String face = request.getParameter("face");
			
			String sql = "update BBSUser set userPwd='"+userPwd+"',sex='"+sex+"',address='"+address+"'," +
					"birthday='"+birthday+"',email='"+email+"',phone='"+phone+"',msn='"+msn+"',face='"+face+"' where bid="+bid;
			
			flag = bbsusedao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('修改成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('修改失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();
			
			
		}
		
		//更新回帖信息
		if(param.equals("updateReply")){
			

			int rid = Integer.parseInt(request.getParameter("rid"));
			String replyName = request.getParameter("replyName");
			String replyBody = request.getParameter("replyBody");
			
			String sql = "update Reply set replyerName='"+replyName+"',replyBody='"+replyBody+"' where rid="+rid;
			
			
			flag = replyDao.executeSQL(sql);
			if(flag){
				out.write("<script>alert('修改成功');location.href='javascript:history.go(-2)'</script>");
			}else{
				out.write("<script>alert('修改失败');location.href='javascript:history.go(-1)'</script>");
				}
			out.close();

		}

	}

}
